package com.caishi.lkx.exam.service.impl;

import com.caishi.lkx.exam.QuestionCollection;
import com.caishi.lkx.exam.ienum.type.QuestionCollectionType;

import com.caishi.lkx.exam.service.QuestionCollectionService;
import com.caishi.lkx.exam.util.QuestionUtil;
import com.caishi.lkx.exam.vo.QuestionCollectStructureVo;
import com.caishi.lkx.exam.vo.QuestionCollectVo;

import javax.annotation.Resource;
import java.io.Serializable;

public abstract class AbstractQuestionCollectionService<T extends QuestionCollection<T, ID>, ID extends Serializable> implements QuestionCollectionService<T, ID> {

    @Resource
    protected CoreQuestionCollectionService coreQuestionCollectionService;

    @Override
    public T insert(T entity) {
        entity.setQcount(QuestionUtil.getCollectionQuestionCount(entity.getContent()));
        return QuestionCollectionService.super.insert(entity);
    }

    @Override
    public T update(T entity) {
        entity.setQcount(QuestionUtil.getCollectionQuestionCount(entity.getContent()));
        return QuestionCollectionService.super.update(entity);
    }

    @Override
    public T simpleUpdate(T entity) {
        entity.setQcount(QuestionUtil.getCollectionQuestionCount(entity.getContent()));
        return QuestionCollectionService.super.simpleUpdate(entity);
    }

    @Override
    public QuestionCollectVo clientCollectionData(String userId, Long groupId, ID id, String industryId, String examId, Long categoryId, boolean setRightAnswer) {
        var model = getById(id);
        if (model == null) return null;

        var result = coreQuestionCollectionService.clientCollectionData(model, industryId, examId, categoryId);
        result.setType(nowCollectionType());
        return result;
    }

    @Override
    public QuestionCollectStructureVo collectionDataStructure(ID id, String industryId, String examId, Long categoryId) {
        var model = getById(id);
        if (model == null) return null;
        var result = coreQuestionCollectionService.collectionDataStructure(model, industryId, examId, categoryId);
        result.setType(nowCollectionType());
        return result;
    }


    abstract QuestionCollectionType nowCollectionType();
}
